package ooboo;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;
import twitter4j.http.AccessToken;

public class Authenticator
{
	private static Hashtable<String,Twitter> twitters = new Hashtable<String,Twitter>();
	private static boolean mention = false;
	
	public static User newUser(AccessToken accessToken) throws TwitterException
	{
		Twitter twitter = newTwitter();
		twitter.setOAuthAccessToken(accessToken);
		User user = twitter.verifyCredentials();
		System.out.println(user.getScreenName());
		twitters.put(user.getScreenName(),twitter);
		return user;
	}
	
	public static void register(String screenName,AccessToken accessToken)
	{
		Twitter twitter = newTwitter();
		twitter.setOAuthAccessToken(accessToken);
		twitters.put(screenName,twitter);
	}
	
	public static Twitter newTwitter()
	{
		Twitter twitter = new Twitter();
		twitter.setOAuthConsumer("LacxJxVEk07F5MBEIVSY4g","OAXNk6FWnSZU6w2MI8oo5W7SOqpLzVM0Wcx0CJ6lqE");
		return twitter;
	}
	
	public static void getTweets(String screenName)
	{
		if (mention) {
			try {
				Storage.store(screenName, twitters.get(screenName).getMentions(new Paging(1)));
			} catch (TwitterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			mention = false;
		} else {
			List<Status> statuses = null;
			ArrayList<List<Status>> newStatuses = new ArrayList<List<Status>>(10);
			
			try {
				long maxID = Storage.getMaxID(screenName);
				if (maxID > 0) {				
					int pageNum = 1;
					
					newStatuses.add(twitters.get(screenName).getFriendsTimeline(new Paging(pageNum, maxID)));
					
					while (!newStatuses.get(pageNum-1).isEmpty() && pageNum <= 10) {
						Ooboo.numNewTweets+=newStatuses.size();
						pageNum++;
						newStatuses.add(twitters.get(screenName).getFriendsTimeline(new Paging(pageNum, maxID)));
					}
					
					for (int i = newStatuses.size()-1; i >=0; i--) {
						if (!(newStatuses.get(i).isEmpty())) {
							Storage.store(screenName, newStatuses.get(i));
							ImageCache.update(newStatuses.get(i));
						}
					}
					
				} else {
					for (int i = 1; i <= 5; i++) {
						statuses = twitters.get(screenName).getFriendsTimeline(new Paging(i));
						Storage.store(screenName, statuses);
						ImageCache.update(statuses);
						Ooboo.numNewTweets+=statuses.size();
					}
				}
			} catch (TwitterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			mention = true;
		}
	}
	
	public static void tweet (String screenName, String tweetText) {
		try {
			twitters.get(screenName).updateStatus(tweetText);
		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void reply(String screenName,String text,long replytoStatusID)
	{
		try
		{
			twitters.get(screenName).updateStatus(text,replytoStatusID);
		}
		catch (TwitterException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
